package dao;

import database.DBConnection;
import java.io.IOException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import model.Aluno;
import util.PropertiesManager;

/**
 *
 * @author Fellipe
 */
public class AlunoDAO implements InterfaceDAO {
  
  private HashMap dados;
  DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
  
  public AlunoDAO() throws IOException{
    dados = new PropertiesManager("sql.properties").readPropertiesFile();
  }

  @Override
  public void inserir(Object obj) throws SQLException {
    Aluno aluno = (Aluno) obj;
    
    Connection conexao = DBConnection.getInstance();    
    //String sql = (String) dados.get("Aluno.Inserir");
    //PreparedStatement stmt = conexao.prepareStatement(sql);
    
    CallableStatement stmt = conexao.prepareCall("{call proc_novoaluno(?, ?, ?)}");
    stmt.setString(1, aluno.getNomeAluno());
    stmt.setString(2, aluno.getNumMatricula());
    stmt.setInt(3, 0);
  
    stmt.execute();
    stmt.close();
  }

  @Override
  public void excluir(Object obj) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }  
 
  @Override
  public ArrayList pesquisarTudo() throws SQLException {
    ArrayList newList = new ArrayList();
      Connection conexao = DBConnection.getInstance();
      String sql = (String) dados.get("Aluno.Tudo");
      PreparedStatement pstmt = conexao.prepareStatement(sql);
      
      ResultSet rs = pstmt.executeQuery();
      while(rs.next()){
          Aluno aluno = new Aluno();
          aluno.setNomeAluno(rs.getString(1));
          aluno.setNumMatricula(rs.getString(2));
          aluno.setTotalHoras(rs.getInt(3));
          newList.add(aluno);
      }
      return newList;
  }

  @Override
  public Object pesquisarChave(int chave) throws SQLException { 
    throw new UnsupportedOperationException("Not supported yet.");
  }

 /* @Override
  public void editar(Object obj) throws SQLException {
        Aluno newAluno = (Aluno) obj;   
        Aluno oldAluno = (Aluno) pesquisarChave(newAluno.getNomeAluno()); 
                     
        Connection conexao = DBConnection.getInstance();
        String sql = (String) dados.get("Aluno.Editar");
        PreparedStatement pstmt = conexao.prepareStatement(sql);
        pstmt.setString(1, newAluno.getDataIngresso());
        pstmt.setString(2, newAluno.getNumMatricula());
        pstmt.setInt(3, oldAluno.getCodPessoa());        
        pstmt.execute();
        pstmt.close();
  }*/
  
  @Override
  public void editar(Object obj) throws SQLException {
    Aluno newAluno = (Aluno) obj;  
    Aluno oldAluno = (Aluno) pesquisarNome(newAluno.getNomeAluno()); 

    Connection conexao = DBConnection.getInstance();
    String sql = (String) dados.get("Aluno.Editar");
    PreparedStatement pstmt = conexao.prepareStatement(sql);
    pstmt.setString(1, newAluno.getNomeAluno());
    pstmt.setString(2, oldAluno.getNomeAluno());
    pstmt.execute();
    pstmt.close();
  }
  
  public Object pesquisarNome(String name) throws SQLException {
    Aluno aluno = new Aluno();
    
    Connection conexao = DBConnection.getInstance();
    String sql = (String) dados.get("Aluno.BuscaPorNome");
    PreparedStatement pstmt = conexao.prepareStatement(sql);
    pstmt.setString(1, name);
    ResultSet rs = pstmt.executeQuery();
    if(rs.next()){
        aluno.setNomeAluno(rs.getString(1));
        aluno.setNumMatricula(rs.getString(2));
    }
    return aluno;
  }
}